package com.cook.controller;

import com.cook.dto.recipes.CommentDTO;
import com.cook.result.Result;
import com.cook.service.CommentsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/comments")
@Slf4j
public class CommentsController {

    @Autowired
    private CommentsService commentsService;

    /**
     * 条件分页查询评论
     */
    @GetMapping
    public Result<Map<String, Object>> getCommentsByCondition(
            @RequestParam(required = false) Long recipeId,
            @RequestParam(required = false) Long postId,
            @RequestParam(required = false) Long userId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        log.info("条件查询评论: recipeId={}, postId={}, userId={}, pageNum={}, pageSize={}", recipeId, postId, userId, pageNum, pageSize);

        try {
            Map<String, Object> result = commentsService.getCommentsByCondition(recipeId, postId, userId, pageNum, pageSize);
            return Result.success(result);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("查询评论失败: ", e);
            return Result.error(500, "查询评论失败");
        }
    }

    /**
     * 查询用户评论
     */
    @GetMapping("/user/{userId}")
    public Result<Map<String, Object>> getCommentsByUserId(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        log.info("查询用户评论: userId={}, pageNum={}, pageSize={}", userId, pageNum, pageSize);

        try {
            Map<String, Object> result = commentsService.getCommentsByUserId(userId, pageNum, pageSize);
            return Result.success(result);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("查询用户评论失败: ", e);
            return Result.error(500, "查询评论失败");
        }
    }

    /**
     * 查询所有评论
     */
    @GetMapping("/all")
    public Result<Map<String, Object>> getAllComments(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        log.info("查询所有评论: pageNum={}, pageSize={}", pageNum, pageSize);

        try {
            Map<String, Object> result = commentsService.getAllComments(pageNum, pageSize);
            return Result.success(result);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("查询所有评论失败: ", e);
            return Result.error(500, "查询评论失败");
        }
    }

    /**
     * 添加评论
     */
    @PostMapping
    public Result<Long> addComment(@RequestBody CommentDTO commentDTO) {
        log.info("添加评论: userId={}", commentDTO.getUserId());

        try {
            Long commentId = commentsService.addComment(commentDTO);
            return Result.success("评论添加成功", commentId);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("添加评论失败: ", e);
            return Result.error(500, "添加评论失败");
        }
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/{commentId}")
    public Result<Boolean> deleteComment(
            @PathVariable Long commentId,
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) Long recipeId,
            @RequestParam(required = false) Long postId) {

        log.info("删除评论: commentId={}, userId={}, recipeId={}, post极Id={}", commentId, userId, recipeId, postId);

        try {
            boolean success = commentsService.deleteComment(commentId, userId, recipeId, postId);
            if (success) {
                return Result.success("评论删除成功", true);
            } else {
                return Result.error("评论删除失败");
            }
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("删除评论失败: ", e);
            return Result.error(500, "删除评论失败");
        }
    }

    /**
     * 统计用户评论数量
     */
    @GetMapping("/count/user/{userId}")
    public Result<Integer> getCommentsCountByUserId(@PathVariable Long userId) {
        log.info("统计用户评论数量: userId={}", userId);

        try {
            int count = commentsService.getCommentsCountByUserId(userId);
            return Result.success(count);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("统计用户评论数量失败: ", e);
            return Result.error(500, "统计评论数量失败");
        }
    }

    /**
     * 统计食谱评论数量
     */
    @GetMapping("/count/recipe/{recipeId}")
    public Result<Integer> getCommentsCountByRecipeId(@PathVariable Long recipeId) {
        log.info("统计食谱评论数量: recipeId={}", recipeId);

        try {
            int count = commentsService.getCommentsCountByRecipeId(recipeId);
            return Result.success(count);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("统计食谱评论数量失败: ", e);
            return Result.error(500, "统计评论数量失败");
        }
    }

    /**
     * 统计动态评论数量
     */
    @GetMapping("/count/post/{postId}")
    public Result<Integer> getCommentsCountByPostId(@PathVariable Long postId) {
        log.info("统计动态评论数量极: postId={}", postId);

        try {
            int count = commentsService.getCommentsCountByPostId(postId);
            return Result.success(count);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("统计动态评论数量失败: ", e);
            return Result.error(500, "统计评论数量失败");
        }
    }

    /**
     * 统计所有评论数量
     */
    @GetMapping("/count/all")
    public Result<Integer> getAllCommentsCount() {
        log.info("统计所有评论数量");

        try {
            int count = commentsService.getAllCommentsCount();
            return Result.success(count);
        } catch (Exception e) {
            log.error("统计所有评论数量失败: ", e);
            return Result.error(500, "统计评论数量失败");
        }
    }
}
